package com.woniuxy.businessassembly.controller;

import com.woniuxy.businessassembly.index.HotelInfoIndex;
import com.woniuxy.businessassembly.index.HotelPolicyIndex;
import com.woniuxy.businessassembly.model.HotelInfo;
import com.woniuxy.businessassembly.model.HotelPolicy;
import com.woniuxy.businessassembly.service.IHotelInfoService;
import com.woniuxy.businessassembly.service.IHotelPolicyService;
import com.woniuxy.common.Result;
import com.woniuxy.hotel.api.HotelInfoManagement;
import com.woniuxy.hotel.form.HotelForm;
import com.woniuxy.hotel.form.PolicyForm;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author kevin kwok
 * @create 2021-05-25 10:09
 */
@Slf4j
@RestController
public class HotelInfoManagementController implements HotelInfoManagement {
    @Resource
    private IHotelPolicyService policyService;
    @Resource
    private IHotelInfoService hotelInfoService;
    @Resource
    private ElasticsearchOperations eo;

    @Override
    public Result hotelInfoDetails(HotelForm hotelForm) {
        log.info("新增酒店信息{}",hotelForm);
        HotelInfo hotelInfo = new HotelInfo();
        BeanUtils.copyProperties(hotelForm,hotelInfo);
        System.out.println("拷贝后的属性：………………"+hotelInfo);
        int i = hotelInfoService.addHotelInfo(hotelInfo);
        System.out.println("新增对象："+hotelInfo);
        /*新增的酒店信息存入ES中*/
        HotelInfoIndex hotelInfoIndex = new HotelInfoIndex();
        BeanUtils.copyProperties(hotelInfo,hotelInfoIndex);
        HotelInfoIndex save = eo.save(hotelInfoIndex);
        System.out.println("es酒店信息新增成功~！！！！！");

        return new Result(true,"新增酒店展示信息成功！",i);
    }

    @Override
    public Result addHotelPolicy(PolicyForm policyForm) {
        log.info("新增政策{}",policyForm);
        int i = policyService.addHotelPolicy(policyForm);
        return new Result(true,"新增酒店政策成功！",i);
    }

    @Override
    public Result queryHotelPolicy(String hotelName) {
        log.info("查询某个酒店的政策{}",hotelName);
        HotelPolicy hotelPolicy = policyService.queryPolicyHotel(hotelName);
        return new Result(true,"查询成功",hotelPolicy);
    }

    @Override
    public Result modifyHotelPolicy(PolicyForm policyForm) {
        log.info("修改某个酒店的政策{}{}",policyForm);
        HotelPolicy hotelPolicy = new HotelPolicy();
        BeanUtils.copyProperties(policyForm,hotelPolicy);
        int i = policyService.modifyPolicyHotel(hotelPolicy);
        return new Result(true,"修改成功",i);
    }

    @Override
    public Result modifyHotelInfo(HotelForm hotelForm) {
        log.info("修改酒店的展示信息{}{}",hotelForm);
        HotelInfo hotelInfo = new HotelInfo();
        BeanUtils.copyProperties(hotelForm,hotelInfo);
        int i = hotelInfoService.modifyHotelInfo(hotelInfo);
        /*修改的酒店信息，ES中*/
        HotelInfoIndex hotelInfoIndex = new HotelInfoIndex();

        return new Result(true,"修改成功",i);
    }

    @Override
    public Result queryHotelInfo(String hotelName) {
        /*HotelInfo hotelInfo = hotelInfoService.queryHotelInfo(hotelName);*/
        NativeSearchQueryBuilder nsqb = new NativeSearchQueryBuilder();
        nsqb.withQuery(QueryBuilders.matchQuery("hotel_name",hotelName+""));
        NativeSearchQuery nsq = nsqb.build();
        SearchHits<HotelInfoIndex> search = eo.search(nsq, HotelInfoIndex.class);
        return new Result(true,"查询成功",search);
    }

    @Override
    public Result queryHotelInfoByType(Integer typeId) {
        List<HotelInfo> hotelInfos = hotelInfoService.queryHotelInfoByType(typeId);
        return new Result(true,"查询成功",hotelInfos);
    }

    @Override
    public Result deleteInfoById(Integer hotelId) {
        int i = hotelInfoService.deleteInfoById(hotelId);
        /*es根据主键删除*/
        eo.delete(hotelId+"", HotelInfoIndex.class);
        return new Result(true,"删除成功",hotelId);
    }
}
